package com.hmall.common.interceptor;

import cn.hutool.core.util.StrUtil;
import com.hmall.common.utils.UserContext;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserInfoInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1、获取请求头中的user-info用户信息
        String userInfo = request.getHeader("user-info");
        //2、判断是否为空
        if (StrUtil.isNotBlank(userInfo)){
            //3、不为空，解析
            UserContext.setUser(Long.valueOf(userInfo));
//            System.out.println("公共类解析用户信息id："+ userInfo);
        }
        //4、放行
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        UserContext.removeUser();
    }
}
